package m.jclecai.com.pay;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Request;
import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;

import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/12/29.
 *
 * 威富通的支付方式；
 */

public class WeiFuTongPayImpl extends BasePayImpl {

    private String mUserName;
    private String mPassword;
    private String mPayMoney;
    private PayChannelBean mChannelBean;
    private String mTradeType;

    public WeiFuTongPayImpl(BaseActivity activity,String userName,String password) {
        super(activity);
        mUserName=userName;
        mPassword=password;
    }

    @Override
    public void pay(PayChannelBean channelBean,String payMoney) {
        setPaying(false);
        mChannelBean = channelBean;
        mPayMoney=payMoney;
        if("alipay".equals(channelBean.getLogo())){
            //这里表示支付宝的支付方式；
            mTradeType = "pay.alipay.native.towap";
        }else{
            //这里的话是QQ的支付方式；
            mTradeType= MainApplication.PAY_QQ_WAP;
        }
//        genOutTradNo(channelBean);
        showDialog();
        getOrderInfo(channelBean);
    }

    /**
     * 创建订单；
     */
    void genOutTradNo(final PayChannelBean payChannelBean){
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("channel", payChannelBean.getChannel());
        pair.put("user", mUserName);
        pair.put("password", mPassword);
        pair.put("money", String.valueOf(mPayMoney));
        pair.put("appstore", ChannelUtil.getChannel());

        System.out.println("创建订单："+pair);
        HttpManager.postAsyn(Constant_Url.CHARGE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay(getString(R.string.error_network));
                dissDialog();
            }

            @Override
            public void onResponse(String response) {
                JSONObject obj = FastOk.get(response, JSONObject.class);
                dissDialog();
                if(obj != null) {
                    if (obj.getString("status").equals("0")) {

                        setOrderNumber(obj.getString("projectno"));
                        String tradNo = obj.getString("projectno");
                        String  ipaddr = obj.getString("ipaddr");
//                        getOrderInfo(tradNo,ipaddr,payChannelBean);
                    }else {
                        mActivity.DisPlay(obj.getString("message"));
                    }
                }else {
                    DisPlay(getString(R.string.error_network));
                }
            }
        }, pair);
    }


    /**
     * * 获取订单信息
     */
    void getOrderInfo(PayChannelBean payChannelBean) {
        BigDecimal bd =new BigDecimal(mPayMoney);
        HashMap<String, String> pair = new HashMap<>();
        pair.put("total_fee", String.valueOf(bd.intValue() * 100));
        pair.put("out_trade_no", payChannelBean.getProjectno());
        pair.put("method", "submitOrderInfo");
        pair.put("mch_create_ip", payChannelBean.getIpaddr());
        pair.put("body", mUserName + "充值");
        pair.put("attach", "附加信息");

        System.out.println("订单信息：" + pair);
        HttpManager.postAsyn("payment/" + payChannelBean.getKey() + "/request.php", new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay(getString(R.string.error_network));
                dissDialog();
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                setPaying(true);
                JSONObject jsonObject = FastOk.get(response, JSONObject.class);
                if (jsonObject == null) {
                    DisPlay("支付失败");
                    return;
                }
                if (TextUtils.isEmpty(jsonObject.getString("token_id"))) {
                    DisPlay("支付失败");
                    return;
                }
                RequestMsg msg = new RequestMsg();
                msg.setTokenId(jsonObject.getString("token_id"));
                //这里是支付宝的支付渠道；
                msg.setTradeType(mTradeType);
                setPaying(true);
                PayPlugin.unifiedH5Pay(mActivity, msg);
            }
        }, pair);
    }
}
